'use strict'

import {app, dialog} from 'electron'
import initWindow from './services/windowManager'
import DisableButton from './config/DisableButton'
import electronDevtoolsInstaller, {VUEJS_DEVTOOLS} from 'electron-devtools-installer'
import argument  from "./config/argument";

// 禁用硬件加速, 禁用GL加速
app.disableHardwareAcceleration()


const gotTheLock = app.requestSingleInstanceLock();
const path = require('path')
const isDevelopment = process.env.NODE_ENV === 'development'


const PROTOCOL = 'electron-vue-slide-upload-demonstrate';
const args = [];//介绍说是win需要的参数

if (!app.isPackaged) {
    args.push(path.resolve(process.argv[1]));
}


args.push('--');
let cols = [];
try {
    const par = process.argv[2]
    if (!isDevelopment) {
        const params = par.split(`${PROTOCOL}://startapp/?`)
        cols.push({params: JSON.stringify(params)})
        if (params.length > 0) {
            const pars = params[1].split('&&')
            if (pars.length !== 0) {
                pars.forEach(item => {
                    let kv = item.split('=')
                    cols.push({key: kv[0], value: kv[1]})
                })
            }
        }
    } else {
        for (const item in argument) {
            cols.push({key: item, value: argument[item]})
        }

    }
} catch (e) {
    console.log(e);
}


function onAppReady() {
    initWindow(cols)
    DisableButton.Disablef12()
    if (isDevelopment) {
        electronDevtoolsInstaller(VUEJS_DEVTOOLS)
            .then((name) => console.log(`installed: ${name}`))
            .catch(err => console.log('Unable to install `vue-devtools`: \n', err))
    }
}

//禁止程序多开，此处需要单例锁的同学打开注释即可
// const gotTheLock = app.requestSingleInstanceLock()
// if(!gotTheLock){
//   app.quit()
// }
app.isReady() ? onAppReady() : app.on('ready', onAppReady)
// 解决9.x跨域异常问题
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')

app.on('window-all-closed', () => {
    // 所有平台均为所有窗口关闭就退出软件
    app.quit()
})
app.on('browser-window-created', () => {
    console.log('window-created')
})
if (!gotTheLock) {
    app.quit();
}

if (process.defaultApp) {
    if (process.argv.length >= 2) {
        app.removeAsDefaultProtocolClient(PROTOCOL)
        console.log('有于框架特殊性开发环境下无法使用')
    }
} else {
    app.setAsDefaultProtocolClient(PROTOCOL, process.execPath, args)
}
